﻿package com.monboo.skin{
	import flash.display.Sprite;

	public class Skin extends Sprite {
		private var map:Array = new Array();
		private var UI:Array = new Array();
		private var classMap:Array = new Array();

		public function Skin() {

		}
		
		public function addClass(cl:Class,cName:String):void{
			classMap[cName] = cl;
		}
		public function addUI(cName:String,uName:String):void{
			UI[uName] = new classMap[cName](); 
		}
		
		public function addMap(mapName:String,path:String):void{
			map[mapName] = path;
		}
		
		public function $c(cName:String):Class{
			return classMap[cName];
		}
		public function $u(uName:String):Sprite{
			return UI[uName];
		}
		public function $(mapName:String):Sprite {
			var arr:Array=map[mapName].split(".");
			var tree:Object= new Object();
			
			if (map[mapName]==null) {
				trace("请先通过addUI方法定义["+mapName+"]对象!");
				return null;
			}
			if (UI[arr[0]]==null) {
				trace("请先通过UIMap方法定义["+arr[0]+"]对象!");
				return null;
			}
			
			for (var i:uint=0; i<arr.length; i++) {
				if (i==0) {
					tree[i]=UI[arr[0]];
				} else {
					tree[i]=tree[i-1].getChildByName(arr[i]);
				}
			}
			return tree[arr.length-1];
		}
	}
}